package com.controller.user;

import com.domain.Member;
import com.domain.Space;
import com.domain.User;
import com.server.MemberService;
import com.server.SpaceService;
import com.server.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(path="/users")
public class loginController {

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "spaceService")
    private SpaceService spaceService;
    @Resource(name = "memberService")
    private MemberService memberService;

    @RequestMapping(path = "/welcome")
    public String index() {
        System.out.println("欢迎登陆知士荟平台...");
        return "welcome";
    }

    //登入验证：
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String usernumber, String password,
                        Model model, HttpSession session) {
        System.out.println("hhhhhhhhhhhh");
        if ((!usernumber.equals("") && usernumber != null)
                && (!password.equals("") && password != null)) {
            // 将用户输入的用户名和密码于数据库中查找的结果对比，判断存在该用户：
            User user = userService.findLoginUser(usernumber, password);
            session.setAttribute("user", user);
            model.addAttribute("user",user);
            if (user != null) {
                System.out.println("****登入成功****");
                System.out.println(user.getUsernumber() + "  " + user.getPassword());
                List<Space> space=spaceService.selectSpaceAll();
                System.out.println(space);
                model.addAttribute("space",space);
                int user_id=user.getId();
                List<Member> memberList=memberService.selectMemberByUserId(user_id);
                List<Space> spaceUser=new ArrayList();
                for(Member member:memberList){
                    int space_id=member.getSpace_id();
                    Space space1=spaceService.selectSpaceById(space_id);
                    spaceUser.add(space1);
                }
//                model.addAttribute("spaceUser",spaceUser);
                return "redirect:main";
            } else {
                model.addAttribute("message", "登录失败，该用户不存在，请进行注册！");
                System.out.println("****登入失败****");
                return "welcome";
            }
        } else {
            return "welcome";
        }
    }

    //登入验证：
    @RequestMapping(value = "/resign", method = RequestMethod.POST)
    public String resign(User user, String usernumber, Model model, HttpSession session) {
        if ((!user.getUsernumber().equals("") && user.getUsernumber() != null)
                && (!user.getPassword().equals("") && user.getPassword() != null)) {
            if(user.getPassword()==null||user.getUsernumber()==null||user.getUsername()==null){
                model.addAttribute("message", "注册失败，信息未填写完全，请重新注册！");
                System.out.println("****注册失败1****");
                return "welcome";
            } else if(!user.getPassword().equals(user.getRePassword())){
                model.addAttribute("message", "注册失败，两次输入密码不一样！");
                System.out.println("****注册失败2****");
                return "welcome";
            }else {
                if(user.getPassword().length()<6||user.getPassword().length()>20){
                    model.addAttribute("message", "注册失败，密码长度应该为6-20位！");
                    System.out.println("****注册失败3****");
                    return "welcome";
                }else{
                    System.out.println("****注册****");
                    if(userService.selectUserByUsernumber(usernumber)!=null){
                        System.out.println("****注册中1****");
                        model.addAttribute("message", "注册失败，该用户已存在！");
                        System.out.println("****注册失败4****");
                        return "welcome";
                    }else if(userService.selectUserByUsernumber(usernumber)==null){
                        user.setStatus("1");
                        user.setUserLogo("member-logo");
                        user.setIntroduce("无简介");
                        System.out.println("****注册中2****");
                        int result = userService.registerUser(user);
                        if (result >= 0) {
                            model.addAttribute("message", "注册成功，请进行登录！");
                            System.out.println("****注册成功****");
                        }
                        return "welcome";
                    }
                    return "welcome";
                }
            }
        }else{
            System.out.println("****注册失败5****");
            return "welcome";
        }
    }

    @RequestMapping(value = "/tuichu")
    public String tuichuUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return "welcome";
    }

    @RequestMapping(value = "/main")
    public String main(Model model, HttpSession session,
                       @RequestParam(value="page",defaultValue="1") int page,
                       @RequestParam(value="pageSize",defaultValue="10") int pageSize) {
        System.out.println("**********login-main显示******");
        User login_user=(User) session.getAttribute("user");
        int id=login_user.getId();
        User user=userService.selectUserById(id);
        session.setAttribute("user", user);
        model.addAttribute("user",user);
        int user_id=id;
        List<Member> memberList=memberService.selectMemberByUserId(user_id);
        List<Space> spaceUser=new ArrayList();
        for(Member member:memberList){
            int space_id=member.getSpace_id();
            Space space1=spaceService.selectSpaceById(space_id);
            spaceUser.add(space1);
        }
        model.addAttribute("spaceUser",spaceUser);
        System.out.println("spaceUser大小"+spaceUser.size());
        System.out.println("spaceUser===="+spaceUser);
        //分页：
        List<Space> spaceList=spaceService.findAllSpace(page, pageSize);
        model.addAttribute("spaceList",spaceList);
        //计算总页数：
        int spaceTotal=spaceService.findSpaceTotal();
        System.out.println("登陆显示总数为：===="+spaceTotal);
        if((spaceTotal/pageSize)<1){
            model.addAttribute("pageTotal",1);
        }else{
            System.out.println("登陆spaceTotal%pageSize==="+(spaceTotal%pageSize));
            if(spaceTotal%pageSize!=0){
                int pageTotal=new Integer(spaceTotal/pageSize+1);
                model.addAttribute("pageTotal",pageTotal);
            }else{
                int pageTotal=new Integer(spaceTotal/pageSize);
                model.addAttribute("pageTotal",pageTotal);
            }
        }
        //设置当前为第一页：
        model.addAttribute("currentPage",page);
        return "main";
    }

}
